# Created by 黄景涛
# DATE: 2025/5/21

import logging

import yaml
from logging import getLogger
from jsonpath_ng import parse

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=logging.DEBUG
)

logger = getLogger()

with open('data.yaml', mode='r', encoding='utf8') as f:
    json_data = yaml.safe_load(f)

patterns = [
    '$.students[0].name',
    '$..name',
    '$.students[*].school',
    # '$.students[?(@.age < 18)]',      # 写法不支持
    '$.store.*',
    # '$.students[0,2].name',           # 写法不支持
    # '$.students[1:3:2].name'          # 写法不支持
]

for i, pattern in enumerate(patterns, 1):
    expr = parse(pattern)
    matches = [match.value for match in expr.find(json_data)]
    logger.info(f'eg{i}结果：{matches}')

